package com.threeonemeeting.activity;

import java.util.Map;

import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.snda.lib.http.TaskManager;
import com.snda.lib.ui.MessageBox;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.adapter.EnrollUserInfoListAdapter;
import com.threeonemeeting.list.EnrollUserInfoList;
import com.threeonemeeting.model.EnrollUserInfo;
import com.threeonemeeting.tab.BaseTabActivity;
import com.threeonemeeting.task.GetEnrollUserListTask;
import com.threeonemeeting.task.HttpConst;

public class EnrollUserListActivity extends BaseTabActivity implements
		OnItemClickListener, ICallBack {

	final static int MENU_ID_DELETE = Menu.FIRST;

	private int nMeetingId = 0;
	private EnrollUserInfoListAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_enroll_user_list);
		init("已报名用户");
		nMeetingId = getIntent().getExtras().getInt("id");

		// 编辑框的内容
		final EditText editSearch = (EditText) findViewById(R.id.editSearch);
		editSearch.setText(getResources().getString(
				R.string.string_search_enroll_user));
		editSearch.setOnFocusChangeListener(new OnFocusChangeListener() {

			public void onFocusChange(View v, boolean hasFocus) {
				String strHint = getResources().getString(
						R.string.string_search_enroll_user);
				String strSearch = editSearch.getText().toString();
				if (hasFocus == true) {
					if (strSearch.equalsIgnoreCase(strHint) == true) {
						editSearch.setText("");
					}
				} else {

					if (strSearch.equalsIgnoreCase("") == true) {
						editSearch.setText(strHint);
					}
				}
			}
		});

		// ListView
		ListView listViewEnrollUsers = (ListView) findViewById(R.id.lv_enroll_user_list);
		mAdapter = new EnrollUserInfoListAdapter(this);
		listViewEnrollUsers.setAdapter(mAdapter);
		listViewEnrollUsers.setOnItemClickListener(this);
		listViewEnrollUsers.setCacheColorHint(Color.TRANSPARENT);
		listViewEnrollUsers.setOnCreateContextMenuListener(listViewMenuContext);

		LinearLayout loadingLayout = (LinearLayout) findViewById(R.id.loading_layout);
		LinearLayout listLayout = (LinearLayout) findViewById(R.id.list_layout);
		loadingLayout.setVisibility(View.VISIBLE);
		listLayout.setVisibility(View.GONE);

		// 发送请求
		GetEnrollUserListTask task = new GetEnrollUserListTask(this, this);
		Bundle params = new Bundle();
		params.putString("eid", String.valueOf(nMeetingId));
		params.putString(
				"uid",
				String.valueOf(DataCenter.getInstance().currentLoginUserInfo.nUserId));
		params.putString("pass",
				DataCenter.getInstance().currentLoginUserInfo.strPassword);
		TaskManager.addTask(task, params, true);
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		int nIdex = arg2;
		EnrollUserInfo info = mAdapter.getItem(nIdex);
		if (info == null)
			return;

		Bundle b = new Bundle();
		b.putInt("jid", info.nUserId);
		b.putInt("eid", nMeetingId);
		b.putSerializable("info", info);
		startActivity("EnrollUserDetailActivity", b);
	}

	ListView.OnCreateContextMenuListener listViewMenuContext = new ListView.OnCreateContextMenuListener() {

		public void onCreateContextMenu(ContextMenu menu, View v,
				ContextMenuInfo menuInfo) {
			menu.add(0, MENU_ID_DELETE, 0, "删除该用户");
		}
	};

	// 菜单单击响应
	@Override
	public boolean onContextItemSelected(MenuItem item) {

		switch (item.getItemId()) {

		case MENU_ID_DELETE:
			MessageBox.showToast(this, "delete");
			break;

		}

		return true;
	}

	public void doCallBack(Map<String, Object> mapData) {
		Integer msgType = (Integer) mapData.get(HttpConst.KEY_TASK_TYPE);
		if (msgType == null) {
			return;
		}

		Integer nError = (Integer) mapData.get(HttpUtil.KEY_ERROR_CODE);
		if (nError == null || nError != HttpUtil.ERROR_SUCCESS) {
			return;
		}

		if (msgType == HttpConst.TASKTYPE_GETENROLLUSERS) {

			LinearLayout loadingLayout = (LinearLayout) findViewById(R.id.loading_layout);
			LinearLayout norecordLayout = (LinearLayout) findViewById(R.id.norecord_layout);
			LinearLayout listLayout = (LinearLayout) findViewById(R.id.list_layout);
			loadingLayout.setVisibility(View.GONE);

			EnrollUserInfoList listUserInfo = (EnrollUserInfoList) mapData
					.get("list");

			// 如果评论为空
			if (listUserInfo.size() == 0) {
				norecordLayout.setVisibility(View.VISIBLE);
				listLayout.setVisibility(View.GONE);
				return;
			}

			norecordLayout.setVisibility(View.GONE);
			listLayout.setVisibility(View.VISIBLE);

			mAdapter.setUserList(listUserInfo.listEnrollUsers);
		}
	}
}
